<template>
  <div class="login-container">
    <div class="title-container">
      <h3 class="title">登录</h3>
    </div>
    <div class="contents">
      <div class="imgDiv block">
        <!-- <el-image :src="src"></el-image> -->
        <!--动态将图片轮播图的容器高度设置成与图片一致-->
        <el-carousel height="300px">
          <!--遍历图片地址,动态生成轮播图-->
          <el-carousel-item v-for="(item, index) in img_list" :key="index">
            <!--         <img :src="item.src" alt /> -->
            <el-image :src="item.src"></el-image>
          </el-carousel-item>
        </el-carousel>
      </div>
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        auto-complete="on"
        label-position="left"
      >
        <!-- <div class="title-container">
          <h3 class="title">Login Form</h3>
        </div> -->
        <div class="title-password">
          <span
            :class="{ clickColor: tabClick == 1 }"
            @click="handTab(1)"
            style="border-right:1px solid #d8d8d8 ;"
            >密码登录</span
          >
          <span :class="{ clickColor: tabClick == 2 }" @click="handTab(2)"
            >手机号登录</span
          >
        </div>
        <div v-if="tabClick == 1">
          <el-form-item prop="username">
            <span class="svg-container">
              <svg-icon icon-class="user" />
            </span>
            <el-input
              ref="username"
              v-model="loginForm.username"
              placeholder="Username"
              name="username"
              type="text"
              tabindex="1"
              auto-complete="on"
            />
          </el-form-item>

          <el-form-item prop="password">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="Password"
              name="password"
              tabindex="2"
              auto-complete="on"
              @keyup.enter.native="handleLogin"
            />
            <span class="show-pwd" @click="showPwd">
              <svg-icon
                :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
              />
            </span>
          </el-form-item>
        </div>
        <div v-if="tabClick == 2">
          <el-form-item prop="phoneNumber">
            <span class="svg-container">
              <svg-icon icon-class="user" />
            </span>
            <el-input
              ref="phoneNumber"
              v-model="loginForm.phoneNumber"
              placeholder="用户手机号"
              name="phoneNumber"
              type="text"
              tabindex="1"
              auto-complete="on"
            />
            <span class="show-pwd" @click="verifiy">
              {{ textsse }}
            </span>
          </el-form-item>

          <el-form-item prop="verification">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input
              ref="verification"
              v-model="loginForm.verification"
              type="number"
              placeholder="验证码"
              name="verification"
              tabindex="2"
              auto-complete="on"
              @keyup.enter.native="handleLogin"
            />
          </el-form-item>
        </div>

        <el-button
          :loading="loading"
          type="primary"
          style="width: 100%; margin-bottom: 30px"
          @click.native.prevent="handleLogin"
          >登录</el-button
        >

        <div class="tips">
          <span style="margin-right: 20px">用户名: admin</span>
          <span> 密码: 111111</span>
        </div>
      </el-form>
    </div>
    <div class="footer">
      <div>
        <span>© 2009-2021 Aliyun.com 版权所有 ICP证：浙B2-20080101</span>
      </div>
      <div>
        <span class="ali-report-link-text">浙公网安备 33010602009975号</span>
      </div>
    </div>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('Please enter the correct user name'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('The password can not be less than 6 digits'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: 'yk',
        password: '123abc',
        phoneNumber: '',
        verification: ''
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', validator: validateUsername }
        ],
        password: [
          { required: true, trigger: 'blur', validator: validatePassword }
        ],
        phoneNumber: [
          { required: true, trigger: 'blur', message: '请输入手机号' }
        ],
        verification: [
          { required: true, trigger: 'blur', message: '请输入验证码' }
        ]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      src:
        'https://cube.elemecdn.com/6/94/4d3ea53c084bad6931a56d5158a48jpeg.jpeg',
      img_list: [
        {
          src:
            'https://cube.elemecdn.com/6/94/4d3ea53c084bad6931a56d5158a48jpeg.jpeg'
        },
        {
          src:
            'https://cube.elemecdn.com/6/94/4d3ea53c084bad6931a56d5158a48jpeg.jpeg'
        },
        {
          src:
            'https://cube.elemecdn.com/6/94/4d3ea53c084bad6931a56d5158a48jpeg.jpeg'
        }
      ],
      // 图片父容器高度
      bannerHeight: 1000,
      // 浏览器宽度
      screenWidth: 0,
      tabClick: 1,
      textsse: '发送验证码',
      times: 60,
      isArre: false
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  mounted() {
    // 首次加载时,需要调用一次
    //     this.screenWidth = window.innerWidth;
    //     this.setSize();
    //     // 窗口大小发生改变时,调用一次
    //     window.onresize = () => {
    //       this.screenWidth = window.innerWidth;
    //       this.setSize();
    //     };
  },
  methods: {
    //     setSize: function() {
    //       // 通过浏览器宽度(图片宽度)计算高度
    //       this.bannerHeight = (400 / 1920) * this.screenWidth;
    //     },
    handTab(type) {
      this.tabClick = type
      this.$nextTick(() => {
        this.$refs['loginForm'].clearValidate()
      })
    },
    // 获得验证码
    verifiy() {
      console.log('看看')
      let that = this
      if (that.checkPhone(that.loginForm.phoneNumber) && !that.isArre) {
        // getSms(that.phone).then(res => {
        //   console.log(res);
        //   if (res.success) {
        //     that.$toast("发送成功");
        //   }
        // });
        that.isArre = true
        that.textsse = that.times + '秒'

        let timeInter = setInterval(() => {
          if (that.times > 1) {
            that.isArre = true
            that.times = that.times - 1
            that.textsse = that.times + '秒'
          } else {
            that.textsse = '发送验证码'
            that.isArre = false
            that.times = 60
            clearInterval(timeInter)
          }
        }, 1000)
      }
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 验证手机号
    checkPhone(val) {
      //验证电话号码手机号码，包含至今所有号段? ?
      var ab = /^[1][3,4,5,7,8,9][0-9]{9}$/
      if (ab.test(val) == false) {
        // this.$message({
        //   message: '请输入正确的手机号码',
        //   type: 'success'
        // })
        this.$message.error('请输入正确的手机号码')
        return false
      } else {
        return true
      }
    },
    handleLogin() {
      console.log(this.loginForm)
      this.$refs.loginForm.validate(valid => {
        console.log(valid)
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('user/login', this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || '/' })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */
input[type='number'] {
  -moz-appearance: textfield;
}
input[type='number']::-webkit-inner-spin-button,
input[type='number']::-webkit-outer-spin-button {
  -webkit-appearance: none;
}

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}
/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      color: #73777a;
      height: 47px;
      caret-color: $cursor;
      caret-color: #000;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px #fff inset !important;
        -webkit-text-fill-color: #73777a !important;
      }
    }
  }

  .el-form-item {
    // border: 1px solid rgba(255, 255, 255, 0.1);
    border: 1px solid rgba(214, 214, 214, 1);
    // background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #f5f5f5;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  .contents {
    max-width: 1200px;
    height: 700px;
    padding: 60px 0 80px;
    margin: 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-around;
  }
  .imgDiv {
    width: 450px;
    // padding: 0px 35px 0;
    border-radius: 10px;
    overflow: hidden;
  }
  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    background-color: #fff;
    padding: 20px;
    // padding: 160px 35px 0;
    overflow: hidden;
    // border: 1px solid red;
    box-shadow: 0 2px 15px rgb(0 0 0 / 15%);
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    background: #fff;
    box-shadow: 0 2px 4px 0 rgb(0 0 0 / 10%);
    box-sizing: border-box;
    height: 80px;
    line-height: 80px;
    width: 100%;
    position: relative;
    z-index: 1000;
    clear: both;
    .title {
      font-size: 26px;
      color: #181818;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }
  .title-password {
    height: 50px;
    line-height: 50px;
    display: flex;
    color: #73777a;
    margin-bottom: 20px;
    > span {
      width: 50%;
      border-bottom: 1px solid #d8d8d8;
      text-align: center;
    }
    .clickColor {
      color: #409eff;
      border-bottom: 3px solid #409eff;
    }
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  //底部
  .footer {
    margin: 0 20%;
    padding: 20px;
    color: #73777a;
    border-top: 1px solid #ccc;
    font-size: 12px;
  }
}
</style>
